package ctr;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
 
import javax.microedition.io.StreamConnection;
public class CommunicationThread implements Runnable {

 private StreamConnection myConnect;
	 
	    private InputStream IPS;
	    private InputStreamReader reader;
	    private Scanner scanner;
	    private Robot robot;
	    public CommunicationThread(StreamConnection connection)
	    {
	        myConnect = connection;
            try 
            {			
            	 IPS = myConnect.openInputStream();
            	 reader = new InputStreamReader(IPS);
            	 scanner = new Scanner(reader);
            	
			} 
            catch (IOException e)
            {
            	e.printStackTrace();
			}
			try {
				robot = new Robot();
			} catch (AWTException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	    }
	    @Override
	    public void run() {
	    	
	    while(true)
	    {
	    		if(scanner.hasNextLine())
	    		{
	    			String command = scanner.nextLine();
	    			System.out.println("Command recived : " + command);
	    			processCommand(command);
	    		}
	    }

	    }
	    private void processCommand(String command) {

	    	Runtime runtime = Runtime.getRuntime();
	            switch (command) {
	                case "shutdown": 
	        	    	
					try
					{
						 runtime.exec("shutdown -s -t 0");
						System.exit(0);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	        	    break;
	        	    
	                case "restart":
	                	try
	                	{
	                		runtime.exec("shutdown -r");
	                		System.exit(0);
	                	}
	                	catch (IOException e) {
						// TODO Auto-generated catch block
	                		e.printStackTrace();
	                	}
	        	    break;
	        	    
	                case "lock":
        	    	
	                	try
	                	{
	                		 runtime.exec("rundll32.exe user32.dll, LockWorkStation");
	                		
	                	} 
	                	catch (IOException e) 
	                	{
					    	e.printStackTrace();
	                	}
        	    
	                	break;
	                	
	                case "arrowLeft":
	                	robot.keyPress(KeyEvent.VK_LEFT);
	                	break;
	        
	                case "arrowRight":
	                	robot.keyPress(KeyEvent.VK_RIGHT);
	                	break;
	                	
	                case "touchpad":
	                	touchpad();
	                	break;
	                default:
	                	break;
	            
                	
	            }
          
}
	    private void touchpad()
	    {
	    	int x = 0;
	    	int y = 0;

	    		if(scanner.hasNextLine())
	    		{
	    			String input = scanner.nextLine();

	    			if(input.equals("leftClick"))
	    			{
	    				robot.mousePress(InputEvent.BUTTON1_MASK);
	    				robot.mouseRelease(InputEvent.BUTTON1_MASK);
	    				touchpad();
	    			}
	    			if(input.equals("rightClick"))
	    			{
	    				robot.mousePress(InputEvent.BUTTON3_MASK);
	    				robot.mouseRelease(InputEvent.BUTTON3_MASK);
	    				touchpad();
	    			}
	    			if(input.contains("Y"))
	    			{
	    				int indexyY = input.indexOf("Y");
	    				String xt = input.substring(3, indexyY);
	    				String yt = input.substring(indexyY+3);
	    				x = Math.round(Float.parseFloat(xt));
	    				y = Math.round(Float.parseFloat(yt));
	    				robot.mouseMove(x, y);
	    				touchpad();
	    			}
	    		}
	    }
	      
}

